#ifndef AUTOFOCUSZIGZAG_H
#define AUTOFOCUSZIGZAG_H

#include "AutoFocusInterface.h"

namespace Qly {



/**
 * @brief The AutoFocusZigZag class 自动对焦类。这个类采取的自动对焦策略如下：
 *     1. 先向上运动，如果清晰度变低就反向运动
 *     2. 之后每次清晰度变低就反向运动，并且速度降低一半。
 *     3. 直到达到指定的折返次数停止运动，并返回对焦结束信号
 */
class AutoFocusZigZag : public AutoFocusInterface
{
public:
    enum STATE {ON_IDLE, ON_UP_TEST, ON_UP_TEST_STOP, ON_MOVE_UP, ON_MOVE_UP_STOP, ON_MOVE_DOWN, ON_MOVE_DOWN_STOP, ON_FINISH, ON_STOP};
    explicit AutoFocusZigZag(QObject *parent = nullptr);
public slots:
    void startAutoFocus() override;
    void stop() override;

private slots:

    void newFocusValue(QImage& image, double score) override;
    void zMoveFinished(int32_t stopcode) override;
    void on_watchdogTimeout();
    void on_finished();
    void on_stop();
private:
    void startWatchDog(int ms = 0);
    void stopWatchDog();
private:
    double old_score = 0.0;
    double m_current_speed_mmps;
    double m_target_up_mm;
    double m_target_down_mm;
    int m_count = 3;
    bool m_stop = false;
    STATE m_state = ON_IDLE;
    QTimer *m_pWatchDog = nullptr;
};


}


#endif // AUTOFOCUSZIGZAG_H
